package avicit.bdp.dgs.qa.rest;

import avicit.bdp.dgs.qa.dto.JobDTO;
import avicit.bdp.dgs.qa.service.JobService;
import avicit.platform6.core.rest.msg.Muti3Bean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-03-30
 * @类说明：JobRest
 * @修改记录：
 * @注意事项：
 * @主要功能：质量作业Rest接口
 */
@RestController
@Api(tags = "qa", description = "质量作业Rest接口")
@RequestMapping("/api/bdp/dgs/qa/job")
public class JobRest {
    @Autowired
    private JobService jobService;

    /**
     * 新增质量作业
     *
     * @param jobDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增质量作业")
    public ResponseMsg<JobDTO> addJob(@RequestBody JobDTO jobDTO) {
        ResponseMsg<JobDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.addJob(jobDTO));

        return responseMsg;
    }

    /**
     * 自动创建文件校验质量作业
     *
     * @param map
     * @return
     */
    @PostMapping("/autoCreate")
    @ApiOperation(value = "自动创建文件校验质量作业")
    public ResponseMsg<String> autoCreateFileJob(@RequestBody Map<String, Object> map) {
        return this.jobService.autoCreateFileJob(map);
    }

    /**
     * 删除/批量删除质量作业，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除质量作业，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteJob(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.deleteJob(id));

        return responseMsg;
    }

    /**
     * 修改质量作业
     *
     * @param jobDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改质量作业")
    public ResponseMsg<Boolean> updateJob(@RequestBody JobDTO jobDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.updateJob(jobDTO));

        return responseMsg;
    }

    /**
     * 更新质量作业状态
     *
     * @param bean，第一个参数：jobId（processDefinitonId），第二个参数：processInstanceId，第三个参数：status
     * @return
     */
    @PutMapping("/update-status")
    @ApiOperation(value = "修改质量作业")
    public ResponseMsg<Boolean> updateJobStatus(@ApiParam(value = "修改对象", name = "bean") @RequestBody Muti3Bean<String, String, Integer> bean) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.updateJobStatus(bean));

        return responseMsg;
    }

    /**
     * 查询质量作业详情
     *
     * @param id 质量作业ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询质量作业详情")
    public ResponseMsg<JobDTO> getJobDetail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<JobDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.getJobDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询质量作业
     *
     * @param measureType
     * @param keyWords
     * @param schedulerType
     * @param resultType
     * @param state
     * @param datasourceName
     * @param tableName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询质量作业")
    public ResponseMsg<QueryRespBean<JobDTO>> queryJob(
            @ApiParam(value = "监控类型", name = "measureType") @RequestParam Integer measureType,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "调度方式", name = "schedulerType") @RequestParam(required = false) Integer schedulerType,
            @ApiParam(value = "结果", name = "resultType") @RequestParam(required = false) Integer resultType,
            @ApiParam(value = "状态", name = "state") @RequestParam(required = false) Integer state,
            @ApiParam(value = "数据源名称", name = "datasourceName") @RequestParam(required = false) String datasourceName,
            @ApiParam(value = "表名称", name = "tableName") @RequestParam(required = false) String tableName,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<JobDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryJob(keyWords, measureType, schedulerType, resultType, state, datasourceName, tableName, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 判读模板名称是否重复
     * 注意：
     * 1、同一类型质量作业，名字不能重复
     *
     * @param jobId
     * @param name
     * @param measureType
     * @return true:重复；false:不重复
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "判读质量作业名称是否重复")
    public ResponseMsg<Boolean> isSameName(@ApiParam(value = "作业ID", name = "jobId") @RequestParam(required = false) String jobId,
                                           @ApiParam(value = "名字", name = "name") @RequestParam String name,
                                           @ApiParam(value = "监控类型", name = "measureType") @RequestParam Integer measureType) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.isSameName(jobId, name, measureType));

        return responseMsg;
    }

    /**
     * 启动/停止/重新运行质量作业
     *
     * @param jobId             作业ID，多个ID使用英文逗号隔开
     * @param processInstanceId 流程实例ID
     * @param ruleIdList        规则ID列表（可选参数，支持两种启动方式：1、作业启动；2、指定作业下的规则启动）
     * @param mode              执行模式，0：启动、1：停止、2：重新运行
     * @param userOptions       用户配置参数
     * @return
     */
    @PostMapping("/execute")
    @ApiOperation(value = "启动/停止/重新运行/质量作业")
    public ResponseMsg<String> executeJob(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam String jobId,
                                          @ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId,
                                          @ApiParam(value = "规则列表", name = "ruleIdList") @RequestParam(required = false) List<String> ruleIdList,
                                          @ApiParam(value = "0：启动、1：停止、2：重新运行", name = "mode") @RequestParam Integer mode,
                                          @RequestParam(required = false) Map<String, Object> userOptions) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.executeJob(jobId, processInstanceId, ruleIdList, mode, userOptions));

        return responseMsg;
    }

    /**
     * 启用定时/停用定时
     *
     * @param jobId        作业ID，多个ID使用英文逗号隔开
     * @param releaseState 0：停用定时、1：启用定时
     * @return
     */
    @PostMapping("/release")
    @ApiOperation(value = "启动/停止定时")
    public ResponseMsg<String> releaseJob(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam String jobId,
                                          @ApiParam(value = "0：停用定时、1：启用定时", name = "releaseState") @RequestParam Integer releaseState) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.releaseJob(jobId, releaseState));

        return responseMsg;
    }

    /**
     * 查看日志
     *
     * @param jobId             根据作业ID查询最近一次执行日志
     * @param processInstanceId 根据流程实例ID查询历史任务执行日志
     * @param limit             跳过行数
     * @param skipLineNum       查询的行数
     * @return
     */
    @GetMapping("/log")
    @ApiOperation(value = "查看日志")
    public ResponseMsg<String> queryLog(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam(required = false) String jobId,
                                        @ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId,
                                        @ApiParam(value = "跳过行数", name = "skipLineNum", required = true) @RequestParam(defaultValue = "0", required = false) Integer skipLineNum,
                                        @ApiParam(value = "查询的行数", name = "limit", required = true) @RequestParam(defaultValue = "100000", required = false) Integer limit) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryLog(jobId, processInstanceId, skipLineNum, limit));

        return responseMsg;
    }

    /**
     * 查看质量报告
     *
     * @param jobId             根据质量作业ID查询最近一次执行结果
     * @param processInstanceId 根据流程实例ID查询历史任务执行结果
     * @return
     */
    @GetMapping("/report")
    @ApiOperation(value = "查看质量报告")
    public ResponseMsg<Map<String, Object>> queryReport(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam(required = false) String jobId,
                                                        @ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryReport(jobId, processInstanceId));

        return responseMsg;
    }

    /**
     * 下载质量报告
     *
     * @param jobId             根据质量作业ID下载最近一次执行结果
     * @param processInstanceId 根据流程实例ID下载历史任务执行结果
     * @return
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载质量报告")
    public void downloadReport(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam(required = false) String jobId,
                               @ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId,
                               HttpServletResponse response) {
        this.jobService.downloadReport(jobId, processInstanceId, response);
    }

    /**
     * 查看脏数据
     *
     * @param jobId             根据质量作业ID查看最近一次执行的脏数据
     * @param processInstanceId 根据流程实例ID查看历史任务脏数据
     * @param ruleId            规则ID
     * @return
     */
    @GetMapping("/dirty")
    @ApiOperation(value = "查看脏数据")
    public ResponseMsg<Map<String, Object>> queryDirtyData(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam(required = false) String jobId,
                                                           @ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId,
                                                           @ApiParam(value = "规则ID", name = "ruleId") @RequestParam(required = false) String ruleId) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryDirtyData(jobId, processInstanceId, ruleId));

        return responseMsg;
    }

    /**
     * 查询历史任务
     *
     * @param id
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/history")
    @ApiOperation(value = "查询历史任务")
    public ResponseMsg<QueryRespBean<JobDTO>> queryHistoryTask(@ApiParam(value = "质量作业ID", name = "id") @RequestParam(required = false) String id,
                                                               @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                               @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<JobDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryHistoryTask(id, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 查询监控任务
     *
     * @param id
     * @param jobId
     * @param keyWords
     * @param startTime
     * @param endTime
     * @param state
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/monitor")
    @ApiOperation(value = "查询监控任务")
    public ResponseMsg<QueryRespBean<JobDTO>> queryMonitorTask(@ApiParam(value = "项目ID", name = "id") @RequestParam(required = false) String id,
                                                               @ApiParam(value = "流程定义Id", name = "jobId") @RequestParam(required = false) String jobId,
                                                               @ApiParam(value = "任务名称", name = "keyWords") @RequestParam(required = false) String keyWords,
                                                               @ApiParam(value = "开始时间", name = "startTime") @RequestParam(required = false) String startTime,
                                                               @ApiParam(value = "结束时间", name = "endTime") @RequestParam(required = false) String endTime,
                                                               @ApiParam(value = "任务状态", name = "state") @RequestParam(required = false) Integer state,
                                                               @ApiParam(value = "分页页码", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                               @ApiParam(value = "分页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<JobDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.queryMonitorTask(null, jobId, keyWords, startTime, endTime, state, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 删除/批量删除质量作业，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMonitorTask")
    @ApiOperation(value = "删除/批量删除质量作业，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteMonitorTask(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.jobService.deleteMonitorTask(id));

        return responseMsg;
    }
}
